package com.sina.common.value;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.sina.common.util.AppUtils;
import com.sina.common.util.LogUtils;
import com.sina.common.util.SPUtils;
import com.sina.common.util.bean.LBSInfo;
import com.sina.common.util.bean.UserData;


/**
 * 全局参数
 */
@SuppressWarnings({"unused", "StaticFieldLeak" , "RedundantIfStatement"})
public class StaticValue {

    public static final boolean mShowError=true;
    private static String mLat = "";
    private static String mLon = "";

    public static Application getApp() {
        return AppUtils.getApp();
    }

    public static String getUserAgent() {
        String verName = "";
        try {
            verName = getApp().getPackageManager().getPackageInfo(getApp().getPackageName(), PackageManager.COMPONENT_ENABLED_STATE_DEFAULT).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            LogUtils.e(e.getMessage());
        }
        return "APP-Android-"+ verName +"-"+ Build.VERSION.SDK_INT+"-"+ Build.BRAND+"-"+ Build.MODEL;
    }

    public static String getAppName(Context context) {
        PackageManager pm = context.getPackageManager();
        return context.getApplicationInfo().loadLabel(pm).toString();
    }

    public static void saveLBS(LBSInfo location) {
        if (location != null) {
            if (!TextUtils.isEmpty(location.getLat())) {
                mLat = location.getLat();
            }
            if (!TextUtils.isEmpty(location.getLon())) {
                mLon = location.getLon();
            }
        }
    }

    public static boolean isLogin() {
        UserData userData = getUser();
        if (userData != null && !TextUtils.isEmpty(userData.getUserPhone())) {
            return true;
        } else {
            return false;
        }
    }

    public static void jumpLogin() {
        Uri uri = Uri.parse("login://com.sinafenqi.falsh/path");
        Intent schemeIntent = new Intent();
        schemeIntent.setAction(Intent.ACTION_VIEW);
        schemeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        schemeIntent.setData(uri);
        PackageManager packageManager = StaticValue.getApp().getPackageManager();
        ComponentName componentName = schemeIntent.resolveActivity(packageManager);
        // 判断该协议是否存在
        if (componentName != null) {
            StaticValue.getApp().startActivity(schemeIntent);
        }
    }

    public static UserData getUser() {
        return (UserData) SPUtils.readObj(StaticValue.getApp(), "UserData");
    }

    public static String getLat() {
        return mLat;
    }

    public static String getLon() {
        return mLon;
    }

    public static void firstStart() {
        SPUtils.put("firstStart", "true");
    }

    public static boolean isFirst() {
        String isFirst = (String) SPUtils.get("firstStart", "false");
        if (TextUtils.isEmpty(isFirst) || isFirst.equals("false")) {
            return true;
        } else {
            return false;
        }
    }
}
